package org.xeustechnologies.jcksum.algo;

import org.xeustechnologies.jcksum.Checksum;

public class Fletcher32 implements Checksum {
    int sum1 = 0;
    int sum2 = 0;

    public byte[] getBytes() {
        return null;
    }

    public String getStringValue() {
        return null;
    }

    public long getValue() {
        return ( ( sum2 << 16 ) | sum1 );
    }

    public void reset() {
        sum1 = 0;
        sum2 = 0;
    }

    public void update(int b) {
        sum1 += (byte) b;

        if( sum1 >= 0x0000FFFF ) {
            sum1 -= 0x0000FFFF;
        }

        sum2 += sum1;

        if( sum2 >= 0x0000FFFF ) {
            sum2 -= 0x0000FFFF;
        }

    }

    public void update(byte[] b, int off, int len) {
        for( int i = 0; i < off + len; i++ ) {
            update( b[i] );
        }
    }
}
